package com.alpha.eceasy.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alpha.eceasy.exception.BizException;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.mall.api.dto.MallDemandDTO;
import com.alpha.eceasy.mall.api.entity.MallCustomer;
import com.alpha.eceasy.mall.api.entity.MallDemand;
import com.alpha.eceasy.mall.api.entity.MallDemandDetail;
import com.alpha.eceasy.mall.api.entity.MallOrder;
import com.alpha.eceasy.mall.api.vo.MallDemandVO;
import com.alpha.eceasy.mall.mapper.MallDemandMapper;
import com.alpha.eceasy.mall.service.MallDemandService;
import com.alpha.eceasy.mall.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MallDemandServiceImpl extends BaseServiceImpl<MallDemandMapper, MallDemand>
        implements MallDemandService {
    @Resource
    private MallDemandDetailServiceImpl demandDetailService;
    @Resource
    private MallCustomerServiceImpl customerService;

    public IPage<MallDemandVO> getPageVO(Page<MallDemand> page, MallDemandDTO dto) {
        LambdaQueryWrapper<MallDemand> wrapper = Wrappers.lambdaQuery();
        SecurityUtils.getRoles().forEach(role -> {
                    switch (role) {
                        case "INNER" -> {
                            if (StrUtil.isNotBlank(dto.getSellerGuid())) {
                                List<String> customerGuidList = customerService.list(Wrappers.<MallCustomer>lambdaQuery()
                                                .eq(MallCustomer::getSellerGuid, dto.getSellerGuid()))
                                        .stream().map(MallCustomer::getGuid).toList();
                                wrapper.in(MallDemand::getCustomerGuid, customerGuidList.isEmpty() ? List.of("") : customerGuidList);
                            } else {
                                List<String> customerGuidList = customerService.list(Wrappers.<MallCustomer>lambdaQuery()
                                                .isNull(MallCustomer::getSellerGuid))
                                        .stream().map(MallCustomer::getGuid).toList();
                                wrapper.in(MallDemand::getCustomerGuid, customerGuidList.isEmpty() ? List.of("") : customerGuidList);
                            }
                        }
                        case "USER" -> wrapper.eq(MallDemand::getCustomerGuid, SecurityUtils.getUserGuid());
                    }
                }
        );
        return this.page(page, wrapper)
                .convert(demand -> {
                    MallDemandVO vo = new MallDemandVO(demand);
                    vo.setDemandDetailList(demandDetailService.list(Wrappers.<MallDemandDetail>lambdaQuery()
                            .eq(MallDemandDetail::getMallDemandGuid, demand.getGuid())));
                    return vo;
                });
    }

    public Boolean saveDTO(MallDemandDTO dto) {
        MallDemand demand = new MallDemand();
        if (SecurityUtils.getRoles().contains("USER")) {
            demand.setCustomerGuid(SecurityUtils.getUserGuid());
        }
        if (!this.save(demand)) {
            throw new BizException("保存失败");
        }
        return dto.getDemandDetailList()
                .stream().peek(detail -> detail.setMallDemandGuid(demand.getGuid()))
                .allMatch(detail -> demandDetailService.save(detail));
    }
}
